package com.ems.sys.service.impl;
import com.ems.sys.dao.SalaryMapper;
import com.ems.sys.pojo.Salary;
import com.ems.sys.service.SalaryService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;
import tk.mybatis.mapper.entity.Example;
import java.util.List;
/****
 * @Author:传智播客
 * @Description:Salary业务层接口实现类
 * @Date 2019/6/14 0:16
 *****/
@Service
public class SalaryServiceImpl implements SalaryService {

    @Autowired
    private SalaryMapper salaryMapper;


    /**
     * Salary条件+分页查询
     * @param salary 查询条件
     * @param page 页码
     * @param size 页大小
     * @return 分页结果
     */
    @Override
    public PageInfo<Salary> findPage(Salary salary, int page, int size){
        //分页
        PageHelper.startPage(page,size);
        //搜索条件构建
        Example example = createExample(salary);
        //执行搜索
        return new PageInfo<Salary>(salaryMapper.selectByExample(example));
    }

    /**
     * Salary分页查询
     * @param page
     * @param size
     * @return
     */
    @Override
    public PageInfo<Salary> findPage(int page, int size){
        //静态分页
        PageHelper.startPage(page,size);
        //分页查询
        return new PageInfo<Salary>(salaryMapper.selectAll());
    }

    /**
     * Salary条件查询
     * @param salary
     * @return
     */
    @Override
    public List<Salary> findList(Salary salary){
        //构建查询条件
        Example example = createExample(salary);
        //根据构建的条件查询数据
        return salaryMapper.selectByExample(example);
    }


    /**
     * Salary构建查询对象
     * @param salary
     * @return
     */
    public Example createExample(Salary salary){
        Example example=new Example(Salary.class);
        Example.Criteria criteria = example.createCriteria();
        if(salary!=null){
            // 员工编号
            if(!StringUtils.isEmpty(salary.geteId())){
                    criteria.andEqualTo("eId",salary.geteId());
            }
            // 基本工资
            if(!StringUtils.isEmpty(salary.getBasePay())){
                    criteria.andEqualTo("basePay",salary.getBasePay());
            }
            // 五险一金
            if(!StringUtils.isEmpty(salary.getInsurance())){
                    criteria.andEqualTo("insurance",salary.getInsurance());
            }
            // 效工资绩
            if(!StringUtils.isEmpty(salary.getMeritPay())){
                    criteria.andEqualTo("meritPay",salary.getMeritPay());
            }
            // 津贴
            if(!StringUtils.isEmpty(salary.getSubsidyPay())){
                    criteria.andEqualTo("subsidyPay",salary.getSubsidyPay());
            }
            // 加班费
            if(!StringUtils.isEmpty(salary.getOvertimePay())){
                    criteria.andEqualTo("overtimePay",salary.getOvertimePay());
            }
        }
        return example;
    }

    /**
     * 删除
     * @param id
     */
    @Override
    public void delete(String id){
        salaryMapper.deleteByPrimaryKey(id);
    }

    /**
     * 修改Salary
     * @param salary
     */
    @Override
    public void update(Salary salary){
        salaryMapper.updateByPrimaryKey(salary);
    }

    /**
     * 增加Salary
     * @param salary
     */
    @Override
    public void add(Salary salary){
        salaryMapper.insert(salary);
    }

    /**
     * 根据ID查询Salary
     * @param id
     * @return
     */
    @Override
    public Salary findById(String id){
        return  salaryMapper.selectByPrimaryKey(id);
    }

    /**
     * 查询Salary全部数据
     * @return
     */
    @Override
    public List<Salary> findAll() {
        return salaryMapper.selectAll();
    }
}
